#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 2e4+7;
const int MAXB = 1e6+7;
int dp[MAXN];
int ok[MAXN];
struct node{
    int x,y;
};
node m[MAXB];
bool cmp(node a, node b) {
    if (a.y!=b.y) return a.y<b.y;
    return a.x<b.x;
}
int main(int argc, char** argv) {
#ifdef hai
    //freopen("in.txt", "r", stdin);
#endif
    int n;
    cin>>n;
    int st, en;
    int res = 0;
    dp[1] = 1;
    ok[1] = true;
    for (int i=0; i<n; ++i) {
        scanf("%d %d", &st, &en);
        if (st<en) swap(st, en);
        m[i].x=st; m[i].y=en;
    }
    sort(m, m+n, cmp);
    for (int i=0; i<n; ++i) {
        st = m[i].x; en = m[i].y;
        if (ok[st-1]) {
            if (dp[st]<dp[st-1]+1) {
                dp[st] = dp[st-1] + 1;
                ok[st] = true;
                if (dp[st] > res) res = dp[st];
            }
        }
        if (ok[en-1]) {
            if (dp[en]<dp[en-1]+1) {
                dp[en] = dp[en-1] + 1;
                ok[en] = true;
                if (dp[en] > res) res = dp[en];
            }
        }
    }
    printf("%d\n", res);
    return 0;
}
